-show_edit = true if local_assigns[:show_edit].nil?
-expanded = false if local_assigns[:expanded].nil?
-editable_notes = false if local_assigns[:editable_notes].nil?

=walk_slug_tree(Directive.slugtree(sections), :expanded => expanded, :li_class => 'section') do |item, step, children|
  -controls = item.linked_controls
  -content_id = "content_#{rand(36**8).to_s(36)}" # unique ID for toggle
  -has_children = children.size > 0
  -has_controls = controls.size > 0
  -expandable = has_children || (with_controls && has_controls)
  .item-main{ :'data-object-id' => item.id, :'data-object-type' => item.class.name.underscore }
    .openclose{ :class => [!expandable && 'lastchild', expanded && 'active'] }
    .item-title
      .row-fluid
        =render :partial => 'object_leaf', :locals => { :instance => item, :include_notes => true, :editable_notes => editable_notes, :object_type => 'compliance', :object_icon => 'grcicon-section' }
        .span2
          .objects-count
            -popover_content = capture_haml do
              -if !controls.empty?
                %ul.tree-structure.simple
                  -controls.each do |control|
                    %li.control
                      .item-main
                        .item-title
                          .row-fluid
                            .span12
                              .oneline
                                %a{ :href => flow_control_path(control) }
                                  %span.title=control.title
                                  %span.text-compliance=control.slug
              -elsif item.na?
                .rtf
                  %p.no-data
                    Controls are not applicable to this section
              -else
                .rtf
                  %p.no-data
                    No controls are linked to this section

            -data_popover_trigger = { :placement => "right",
                                 :'popover-trigger' =>  "sticky-hover",
                                 :'original-title' => "<div class='row-fluid'><div class='span12'><h3><i class='grcicon-control-color'></i>Mapped Controls</h3></div></div>".html_safe,
                                 :content => popover_content,
                                 :id => item.id }
            -# NOTE: Changes here should be reflected in app/assets/javascript/mapping/mapping_controller.js
            -#   in the "{section} updated" handler
            %a.controls-count.controllist{:href => "javascript://",
                           :data => data_popover_trigger}
              -if !controls.empty?
                %i.grcicon-control-color
                =controls.size
              -elsif item.na?
                %i.grcicon-control-color
                %small.warning N/A
              -else
                %i.grcicon-control-danger
                %strong.error 0

      .item-actions
        -if show_edit
          %a.btn.btn-mini.pull-right{ :href => edit_flow_section_path(item), :'data-toggle' => 'modal-ajax-form', :'data-modal-reset' => 'reset', :'data-form-target' => 'refresh', :'data-dirty' => "#regulations, #combo", :rel => 'tooltip', :title => "Edit #{item.directive.section_meta_kind.to_s.titleize}", :style => "margin-left:6px" }
            %i.grcicon-edit
        %a.btn.btn-mini.controllist.pull-right{:href => "javascript://", :'data-id'=> item.id , :rel => "tooltip", :title => "Map Controls to Section"}
          !="<i class='grcicon-section-color'></i><i class='grcicon-reg-map'></i><i class='grcicon-control-color'></i>"

  .item-content{ :class => expanded && 'in' }
    -if with_controls
      -if controls.size > 0
        %ul.tree-structure
          =render :partial => 'directives/controls', :locals => { :controls => controls, :show_edit => false }
      -#else
        .no-data
          No controls are linked to this section
